package com.my.study;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 自定义线程池的使用
 */
public class UserThreadPool {

    public static void main(String[] args) {


        //缓存队列设置固定长度为2，为了快速出发rejectHandler,任务队列
        BlockingQueue queue = new LinkedBlockingDeque(2);

        UserThreadFactory f1 = new UserThreadFactory("第一工厂");
        UserThreadFactory f2 = new UserThreadFactory("第二工厂");
        //自定义拒绝cel
        UserRejectHandler userRejectHandler = new UserRejectHandler();

        ThreadPoolExecutor threadPoolFirst = new ThreadPoolExecutor(1,2,60, TimeUnit.SECONDS,queue, f1, userRejectHandler);
        ThreadPoolExecutor threadPoolSecond = new ThreadPoolExecutor(1,2,60, TimeUnit.SECONDS,queue, f2, userRejectHandler);

        Runnable task = new Task();
        for(int i =0; i< 400;i++){
            threadPoolFirst.execute(task);

            threadPoolSecond.execute(task);
        }
    }
}
